package com.maojianjie.aimee.webautotest.browser;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.maojianjie.aimee.webautotest.Browser;

public class RemoteMobile extends Browser
{
	private String remoteAddress;
	private String deviceName;

	public RemoteMobile(String remoteAddress)
	{
		this.remoteAddress = remoteAddress;
		this.deviceName = "iPhone X";
	}
	
	public RemoteMobile(String remoteAddress, String deviceName)
	{
		this.remoteAddress = remoteAddress;
		this.deviceName = deviceName;
	}

	@Override
	protected WebDriver createWebDriver()
	{
		try
		{
			Map<String, String> mobileEmulation = new HashMap<String, String>();
			mobileEmulation.put("deviceName", deviceName);
			Map<String, Object> chromeOptions = new HashMap<String, Object>();
			chromeOptions.put("mobileEmulation", mobileEmulation);
			DesiredCapabilities capabilities = DesiredCapabilities.chrome();
			capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
			
			WebDriver webDriver = new RemoteWebDriver(new URL(remoteAddress), capabilities);
			webDriver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
			webDriver.manage().timeouts().setScriptTimeout(120, TimeUnit.SECONDS);
			return webDriver;
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		return null;
	}

}
